package com.pk.platform.business.privilege.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.pk.platform.business.core.dao.ITreeMakerDao;
import com.pk.platform.business.privilege.dao.IPrivilegeDao;
import com.pk.platform.business.privilege.service.IPrivilegeService;
import com.pk.platform.domain.privilege.Privilege;
import com.pk.platform.domain.privilege.Role;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.Constant;

public class PrivilegeServiceImpl implements IPrivilegeService {

	private IPrivilegeDao privilegeDao;
	
	private ITreeMakerDao treeMakerDao;
	
	public boolean addPrivilege(Privilege privilege) {
		Privilege p = privilegeDao.get(Privilege.class, privilege.getId());
		if(p == null)
		{
			privilege.setType(Constant.PRIVILEGE);
			privilege.setIsShow(Constant.Y);
			privilegeDao.add(privilege);
			return true;
		}
		return false;
	}

	public void delPrivilege(String id) {
		Privilege pri = privilegeDao.get(Privilege.class, id);
		Set<Role> roleSet = pri.getRoleSet();
		for(Role role : roleSet)
		{
			role.getPrivilegeSet().remove(pri);
		}
		privilegeDao.delete(Privilege.class, id);
	}

	public Privilege queryPrivilegeById(String id) {
		return privilegeDao.get(Privilege.class, id);
	}

	public String getAllPrivilegeTree() {
		String[] field = new String[]{"privilegeName", "id", "parentId", null, null, "sort"};
		String condition = " and isShow = '" + Constant.Y + "'";
		String tree = treeMakerDao.getTree("p_privilege", "1", field, true, false, null, null, condition);
		return tree;
	}

	public String getAssignPrivilegeTree() {
		String[] field = new String[]{"privilegeName", "id", "parentId", null, null, "sort"};
		String condition = " and isShow = '" + Constant.Y + "'";
		String tree = treeMakerDao.getTree("p_privilege", "1", field, true, true, "privilegeCb", "privilegeCbClick", condition);
		return tree;
	}

	public void updatePrivilege(Privilege privilege) {
		Privilege pri = privilegeDao.get(Privilege.class, privilege.getId());
		pri.setPrivilegeName(privilege.getPrivilegeName());
		pri.setUrl(privilege.getUrl());
		pri.setSort(privilege.getSort());
	}
	
	public void assignPrivilege(Long roleId, String[] privilegeIds)
	{
		String ids = StringConverter.arrayToString(privilegeIds);
		Role r = privilegeDao.get(Role.class, roleId);
		if(StringConverter.isNotNull(ids))
		{
			List<Privilege> list = privilegeDao.queryPrivilegeByIds(ids);
			r.setPrivilegeSet(new HashSet<Privilege>(list));
		}
		else
		{
			r.setPrivilegeSet(null);
		}
	}
	
	public String hasPrivilegeIds(Long roleId)
	{
		List<Map<String, Object>> list = privilegeDao.queryRoleHasPrivilege(roleId);
		String ids = StringConverter.listToString(list, "privilegeId", "'");
		return ids;
	}
	
	public List<Privilege> querySysPrivilege()
	{
		return privilegeDao.querySysPrivilege();
	}
	
	

	public IPrivilegeDao getPrivilegeDao() {
		return privilegeDao;
	}

	public void setPrivilegeDao(IPrivilegeDao privilegeDao) {
		this.privilegeDao = privilegeDao;
	}

	public ITreeMakerDao getTreeMakerDao() {
		return treeMakerDao;
	}

	public void setTreeMakerDao(ITreeMakerDao treeMakerDao) {
		this.treeMakerDao = treeMakerDao;
	}

}
